package io.renren.modules.weixin.controller;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.modules.weixin.entity.YouhuiNeirongEntity;
import io.renren.modules.weixin.service.YouhuiNeirongService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.weixin.entity.YouhuiBiaotiEntity;
import io.renren.modules.weixin.service.YouhuiBiaotiService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 优惠标题表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-02-02 09:01:33
 */
@RestController
@RequestMapping("weixin/youhui")
@Api("微信小程序-优惠接口")
public class YouhuiController {
    @Autowired
    private YouhuiBiaotiService youhuiBiaotiService;
    @Autowired
    private YouhuiNeirongService youhuiNeirongService;

    @Value("${renren.file.upload}")
    private String upload;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = youhuiBiaotiService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("更新")
    public R update(@RequestBody YouhuiBiaotiEntity youhuiBiaoti) {
        youhuiBiaotiService.updateById(youhuiBiaoti);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    @RequiresPermissions("weixin:youhui:delete")
    public R delete(@RequestBody Integer[] contentIds) {
        youhuiBiaotiService.deleteBatchIds(Arrays.asList(contentIds));

        return R.ok();
    }


    /**
     * 信息
     */
    @GetMapping("/info/{contentId}")
    @ApiOperation("内容信息")
    public R info(@PathVariable("contentId") Integer contentId) {
        EntityWrapper<YouhuiNeirongEntity> condition = new EntityWrapper<>();
        condition.eq("content_id", contentId);
//        YouhuiNeirongEntity youhuiNeirong = youhuiNeirongService.selectById(contentId);
        YouhuiNeirongEntity youhuiNeirong = youhuiNeirongService.selectOne(condition);
        return R.ok().put("youhuiNeirong", youhuiNeirong);
    }

    /**
     * 保存
     */
    @Transactional
    @PostMapping("/save")
    @ApiOperation("保存")
    public R save(@RequestBody YouhuiBiaotiEntity youhuiBiaoti) {
        YouhuiNeirongEntity youhuiNeirong = new YouhuiNeirongEntity();
        youhuiNeirong.setTxt(youhuiBiaoti.getContent());
        String[] files = youhuiBiaoti.getContentImg().split(",");
        youhuiBiaoti.setContentImg("");
        youhuiBiaoti.setContent("");
        youhuiBiaoti.setReleaseDate(new Date());
        youhuiBiaotiService.insert(youhuiBiaoti);

        EntityWrapper<YouhuiBiaotiEntity> condition = new EntityWrapper<>();
        condition.eq("title", youhuiBiaoti.getTitle());
        YouhuiBiaotiEntity youhuiBiaoti1 = youhuiBiaotiService.selectOne(condition);

        youhuiNeirong.setContentId(youhuiBiaoti1.getContentId());
        youhuiNeirong.setTxt1("md");
        youhuiNeirongService.insert(youhuiNeirong);
        File filepath = new File(upload + "/youhui/" + youhuiBiaoti.getContentId().toString());
//            FileUtils.forceMkdir(filepath);
        for (String f : files) {
            f = f.replace("http://tmp/", "").replace("wxfile://", "");
            File file = new File(upload + "/" + f);
            try {
                FileUtils.moveToDirectory(file, filepath, true);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        String newTitleImg = youhuiBiaoti1.getTitleImg().replace("http://tmp/", "https://www.zfj1441.com/youhui/" + youhuiBiaoti.getContentId().toString() + "/")
                .replace("wxfile://", "https://www.zfj1441.com/youhui/" + youhuiBiaoti.getContentId().toString() + "/");
        String newContent = youhuiNeirong.getTxt().replace("http://tmp/", "https://www.zfj1441.com/youhui/" + youhuiBiaoti.getContentId().toString() + "/")
                .replace("wxfile://", "https://www.zfj1441.com/youhui/" + youhuiBiaoti.getContentId().toString() + "/");
        youhuiBiaoti1.setTitleImg(newTitleImg);
        youhuiNeirong.setTxt(newContent);
        youhuiBiaotiService.updateById(youhuiBiaoti1);
        EntityWrapper<YouhuiNeirongEntity> condition1 = new EntityWrapper<>();
        condition1.eq("content_id", youhuiBiaoti.getContentId());
        youhuiNeirongService.update(youhuiNeirong, condition1);
        return R.ok();
    }
}
